package com.shsoft.myThread;

import com.shsoft.colorfulcc.MainActivity;
import com.shsoft.common.Constant;

/**
 * 纯色-呼吸灯 子线程
 */
public class SingleBreathingThread extends Thread{
    private MainActivity activity;
    private int time; // 每次呼吸时间 s
    private int circle; // 单色呼吸次数
    private int currentColorNum;  // 记录当前已经显示的预设颜色的编号
    private int delay; // 有呼吸时间算得的亮度变更间隔 ms
    private int count; // 记录亮度变更次数
    private int currentCircle; // 记录当前的呼吸次数
    private int breathingMode; // 记录呼吸模式
    private int bright;

    public SingleBreathingThread(MainActivity activity) {
        this.activity = activity;
        time = Integer.parseInt(activity.breathing_time.getText().toString());
        circle = Integer.parseInt(activity.breathing_circle.getText().toString());
        bright = Integer.parseInt(activity.breathing_bright.getText().toString());
        delay = (time * 1000) / (bright * 2);
        count = 1;
        currentCircle = 0;
        breathingMode = activity.breathingMode;
    }

    @Override
    public void run() {
        System.out.println("纯色呼吸灯子线程运行");
        while (activity.isPlaying){
            switch (breathingMode){
                case Constant.BREATHING_MODE_D_L_D:
                    doWhileMode_d_l_d();
                    break;
                case Constant.BREATHING_MODE_L_D_L:
                    doWhileMode_l_d_l();
                    break;
                case Constant.BREATHING_MODE_L_D:
                    doWhileMode_l_d();
                    break;
                case Constant.BREATHING_MODE_D_L:
                    doWhileMode_d_l();
                    break;
                default:
                    break;
            }
            try {
                if(breathingMode == Constant.BREATHING_MODE_D_L_D || breathingMode == Constant.BREATHING_MODE_L_D_L){
                    sleep(delay);
                }else {
                    sleep(delay * 2);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                break;
            }
        }
    }

    private void doWhileMode_d_l_d(){
        if(count == 1 && currentCircle == 0){
            // 将灯珠的颜色全部换成要显示的颜色
            for(int i = 0; i < Constant.LIGHT_COUNT; i++){
                activity.lights_4display.put(i,activity.colors.get(currentColorNum));
            }
        }
        // 让主线程刷新灯珠区域
        if(count <= bright){
            activity.alpha = count;
        }else {
            activity.alpha = bright - (count - bright);
        }
        if(activity.alpha * 2 > 255){
            activity.alpha = 255;
        }else {
            activity.alpha = activity.alpha * 2;
        }
        activity.refreshLightsDiv();
        count++;
        if(count == bright * 2){
            count = 1;
            currentCircle++;
            if(currentCircle == circle){
                currentCircle = 0;
                currentColorNum++;
                if(currentColorNum == activity.colors.size()){
                    currentColorNum = 0;
                }
            }
        }
    }
    private void doWhileMode_l_d_l(){
        if(count == 1 && currentCircle == 0){
            // 将灯珠的颜色全部换成要显示的颜色
            for(int i = 0; i < Constant.LIGHT_COUNT; i++){
                activity.lights_4display.put(i,activity.colors.get(currentColorNum));
            }
        }
        // 让主线程刷新灯珠区域
        if(count <= bright){
            activity.alpha = bright + 1 - count;
        }else {
            activity.alpha = count - (bright - 1);
        }
        if(activity.alpha * 2 > 255){
            activity.alpha = 255;
        }else {
            activity.alpha = activity.alpha * 2;
        }
        activity.refreshLightsDiv();
        count++;
        if(count == bright * 2){
            count = 1;
            currentCircle++;
            if(currentCircle == circle){
                currentCircle = 0;
                currentColorNum++;
                if(currentColorNum == activity.colors.size()){
                    currentColorNum = 0;
                }
            }
        }
    }
    private void doWhileMode_l_d(){
        if(count == 1 && currentCircle == 0){
            // 将灯珠的颜色全部换成要显示的颜色
            for(int i = 0; i < Constant.LIGHT_COUNT; i++){
                activity.lights_4display.put(i,activity.colors.get(currentColorNum));
            }
        }
        // 让主线程刷新灯珠区域
        activity.alpha = bright + 1 - count;
        if(activity.alpha * 2 > 255){
            activity.alpha = 255;
        }else {
            activity.alpha = activity.alpha * 2;
        }
        activity.refreshLightsDiv();
        count++;
        if(count == (bright + 1)){
            count = 1;
            currentCircle++;
            if(currentCircle == circle){
                currentCircle = 0;
                currentColorNum++;
                if(currentColorNum == activity.colors.size()){
                    currentColorNum = 0;
                }
            }
        }
    }
    private void doWhileMode_d_l(){
        if(count == 1 && currentCircle == 0){
            // 将灯珠的颜色全部换成要显示的颜色
            for(int i = 0; i < Constant.LIGHT_COUNT; i++){
                activity.lights_4display.put(i,activity.colors.get(currentColorNum));
            }
        }
        // 让主线程刷新灯珠区域
        activity.alpha = count;
        if(activity.alpha * 2 > 255){
            activity.alpha = 255;
        }else {
            activity.alpha = activity.alpha * 2;
        }
        activity.refreshLightsDiv();
        count++;
        if(count == (bright + 1)){
            count = 1;
            currentCircle++;
            if(currentCircle == circle){
                currentCircle = 0;
                currentColorNum++;
                if(currentColorNum == activity.colors.size()){
                    currentColorNum = 0;
                }
            }
        }
    }
}
